﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    [Serializable]
    internal class RenameOverlay
    {
        private string k_RenameOverlayFocusName = "RenameOverlayField";
        [SerializeField]
        private Rect m_EditFieldRect;
        [SerializeField]
        private bool m_IsRenaming;
        [SerializeField]
        private bool m_IsWaitingForDelay;
        [SerializeField]
        private string m_Name;
        [SerializeField]
        private EventType m_OriginalEventType = EventType.Ignore;
        [SerializeField]
        private string m_OriginalName;
        [SerializeField]
        private bool m_UserAcceptedRename;
        [SerializeField]
        private int m_UserData;
        private double s_RenameEndedTime;

        public bool BeginRename(string name, int userData, float delay)
        {
            if ((EditorApplication.timeSinceStartup - this.s_RenameEndedTime) < 0.2)
            {
                return false;
            }
            if (this.m_IsRenaming)
            {
                return false;
            }
            this.m_Name = name;
            this.m_OriginalName = name;
            this.m_UserData = userData;
            this.m_UserAcceptedRename = false;
            this.m_IsWaitingForDelay = delay > 0f;
            this.m_IsRenaming = true;
            if (delay > 0f)
            {
                EditorApplication.CallDelayed(new EditorApplication.CallbackFunction(this.BeginRenameInternalCallback), delay);
            }
            else
            {
                this.BeginRenameInternalCallback();
            }
            return true;
        }

        private void BeginRenameInternalCallback()
        {
            if (this.m_IsRenaming)
            {
                EditorGUI.s_RecycledEditor.content.text = this.m_Name;
                EditorGUI.s_RecycledEditor.SelectAll();
                EditorApplication.RequestRepaintAllViews();
            }
            this.m_IsWaitingForDelay = false;
        }

        public void Clear()
        {
            this.m_IsRenaming = false;
            this.m_OriginalEventType = EventType.Ignore;
        }

        public void EndRename(bool acceptChanges)
        {
            if (this.m_IsRenaming)
            {
                this.m_IsRenaming = false;
                this.m_UserAcceptedRename = acceptChanges;
                EditorApplication.RequestRepaintAllViews();
                this.s_RenameEndedTime = Time.realtimeSinceStartup;
            }
        }

        public bool HasKeyboardFocus()
        {
            return (GUI.GetNameOfFocusedControl() == this.k_RenameOverlayFocusName);
        }

        public bool IsRenaming()
        {
            return this.m_IsRenaming;
        }

        public bool OnEvent()
        {
            if (!this.m_IsRenaming)
            {
                return true;
            }
            this.m_OriginalEventType = Event.current.type;
            if (!this.m_IsWaitingForDelay || ((this.m_OriginalEventType != EventType.MouseDown) && (this.m_OriginalEventType != EventType.KeyDown)))
            {
                return true;
            }
            this.EndRename(false);
            return false;
        }

        public bool OnGUI()
        {
            return this.OnGUI(null);
        }

        public bool OnGUI(GUIStyle textFieldStyle)
        {
            if (!this.m_IsWaitingForDelay)
            {
                if (!this.m_IsRenaming)
                {
                    return false;
                }
                Event current = Event.current;
                if (current.type == EventType.KeyDown)
                {
                    if (current.keyCode == KeyCode.Escape)
                    {
                        current.Use();
                        this.EndRename(false);
                        return false;
                    }
                    if ((current.keyCode == KeyCode.Return) || (current.keyCode == KeyCode.KeypadEnter))
                    {
                        current.Use();
                        this.EndRename(true);
                        return false;
                    }
                }
                if ((this.m_OriginalEventType == EventType.MouseDown) && !this.m_EditFieldRect.Contains(Event.current.mousePosition))
                {
                    this.EndRename(true);
                    return false;
                }
                GUI.changed = false;
                GUIUtility.GetControlID(0x50f6804, FocusType.Passive);
                GUI.SetNextControlName(this.k_RenameOverlayFocusName);
                GUI.FocusControl(this.k_RenameOverlayFocusName);
                GUIStyle textField = EditorStyles.textField;
                if (textFieldStyle == null)
                {
                }
                EditorStyles.s_Current.m_TextField = "PR TextField";
                this.m_Name = EditorGUI.TextField(this.m_EditFieldRect, this.m_Name);
                EditorStyles.s_Current.m_TextField = textField;
                if (current.type == EventType.ScrollWheel)
                {
                    current.Use();
                }
            }
            return true;
        }

        public Rect editFieldRect
        {
            get
            {
                return this.m_EditFieldRect;
            }
            set
            {
                this.m_EditFieldRect = value;
            }
        }

        public bool isWaitingForDelay
        {
            get
            {
                return this.m_IsWaitingForDelay;
            }
        }

        public string name
        {
            get
            {
                return this.m_Name;
            }
        }

        public string originalName
        {
            get
            {
                return this.m_OriginalName;
            }
        }

        public bool userAcceptedRename
        {
            get
            {
                return this.m_UserAcceptedRename;
            }
        }

        public int userData
        {
            get
            {
                return this.m_UserData;
            }
        }
    }
}

